import { ApiPropertyOptional } from '@nestjs/swagger';
import { Type } from 'class-transformer';
import { IsOptional, IsInt, Min, Max, IsIn, IsString } from 'class-validator';
import type { IPaginationQuery } from '@nbase/shared';
import { PAGINATION } from '@nbase/shared';

/**
 * 基础分页查询 DTO
 * 
 * 提供通用的分页字段验证
 * 
 * @example
 * // 直接使用（最简单）
 * export class QueryUserDto extends BasePaginationDto {}
 * 
 * // 覆盖默认排序（如果需要）
 * export class QueryMenuDto extends BasePaginationDto {
 *   override sortOrder?: 'asc' | 'desc' = 'asc';
 * }
 */
export class BasePaginationDto implements IPaginationQuery {
  @ApiPropertyOptional({
    description: '页码',
    example: 1,
    default: PAGINATION.DEFAULT_PAGE,
    minimum: PAGINATION.DEFAULT_PAGE,
  })
  @IsOptional()
  @Type(() => Number)
  @IsInt()
  @Min(PAGINATION.DEFAULT_PAGE)
  page?: number = PAGINATION.DEFAULT_PAGE;

  @ApiPropertyOptional({
    description: '每页数量',
    example: 10,
    default: PAGINATION.DEFAULT_PAGE_SIZE,
    minimum: 1,
    maximum: PAGINATION.MAX_PAGE_SIZE,
  })
  @IsOptional()
  @Type(() => Number)
  @IsInt()
  @Min(1)
  @Max(PAGINATION.MAX_PAGE_SIZE)
  pageSize?: number = PAGINATION.DEFAULT_PAGE_SIZE;

  @ApiPropertyOptional({
    description: '排序字段',
    example: 'createdAt',
  })
  @IsOptional()
  @IsString()
  sortBy?: string;

  @ApiPropertyOptional({
    description: '排序方式',
    example: 'desc',
    enum: ['asc', 'desc'],
    default: 'desc',
  })
  @IsOptional()
  @IsIn(['asc', 'desc'])
  sortOrder?: 'asc' | 'desc' = 'desc';
}

